package com.salefinder.server.server.util;

import java.util.ArrayList;
import java.util.List;

import com.salefinder.client.vo._ConsumerProductVO;
import com.salefinder.client.vo._NameIdPairVO;
import com.salefinder.client.vo._UserItemListVO;
import com.salefinder.client.vo._UserVO;
import com.salefinder.server.server.vo.Brand;
import com.salefinder.server.server.vo.Category;
import com.salefinder.server.server.vo.ConsumerProduct;
import com.salefinder.server.server.vo.Location;
import com.salefinder.server.server.vo.Merchant;
import com.salefinder.server.server.vo.Notes;
import com.salefinder.server.server.vo.User;
import com.salefinder.server.server.vo.UserItem;

public class Mapper {
	
	
	public static _ConsumerProductVO toClientConsumerProduct(ConsumerProduct c){
	
			
		_ConsumerProductVO vo = new _ConsumerProductVO();
		vo.setId(c.getId());
		vo.setCategory(categoryToNameId(c.getProduct().getCategory()));
		vo.setName(c.getProduct().getName());
		vo.setBrand(brandToNameId(c.getBrand()));
		vo.setQty(c.getQty());
		vo.setUnitOfMeasure(c.getUnitOfMeasure());
		vo.setMerchant(merchantToNameId(c.getMerchant()));
		vo.setBasePrice(c.getBasePrice());
		vo.setSalePrice(c.getSalePrice());
		vo.setDateSubmitted(c.getDateSubmitted());
		vo.setDateExpired(c.getDateExpired());
		vo.setPurchaseLocation(locationToNameId(c.getPurchaseLocation()));
		
		// can submit only one note for now
		ArrayList<_NameIdPairVO> notes = new ArrayList<_NameIdPairVO>();
		List<Notes> serverNotes = c.getNotes();
		for(int i = 0; i < serverNotes.size(); i++){
			notes.add(notesToNameId(serverNotes.get(i)));
		}
		vo.setNotes(notes);
		
		return vo;
	}

	public static ConsumerProduct toServerConsumerProduct(_ConsumerProductVO c){
		ConsumerProduct vo = new ConsumerProduct();
		vo.getProduct().setCategory(nameIdToCategory(c.getCategory()));
		vo.getProduct().setName(c.getName());
		vo.setBrand(nameIdToBrand(c.getBrand()));
		vo.setQty(c.getQty());
		vo.setUnitOfMeasure(c.getUnitOfMeasure());
		vo.setMerchant(nameIdToMerchant(c.getMerchant()));
		vo.setBasePrice(c.getBasePrice());
		vo.setSalePrice(c.getSalePrice());
		vo.setDateSubmitted(c.getDateSubmitted());
		vo.setDateExpired(c.getDateExpired());
		vo.setPurchaseLocation(nameIdToLocation(c.getPurchaseLocation()));
		
		// can submit only one note for now
		ArrayList<Notes> notes = new ArrayList<Notes>();
		List<_NameIdPairVO> clientNotes = c.getNotes();
		for(int i = 0; i < clientNotes.size(); i++){
			notes.add(nameIdToNotes(clientNotes.get(i)));
		}
		vo.setNotes(notes);
		return vo;
	}
	
	public static _UserVO toClientUser(User c){
		
		_UserVO vo = new _UserVO();
		vo.setId(c.getId());
		vo.setEmail(c.getEmail());
		vo.setUserId(c.getUserId());
		vo.setPassword(c.getPassword());
		vo.setMyList(toClientUserItemList(c.getMyList()));
		return vo;
		
	}

	public static User toServerUser(_UserVO c){
		User vo = new User();
		vo.setId(c.getId());
		vo.setEmail(c.getEmail());
		vo.setUserId(c.getUserId());
		vo.setPassword(c.getPassword());
		vo.setMyList(toServerUserItemList(c.getMyList()));
	
		return vo;	
	}
	
	public static _UserItemListVO toClientUserItemList(List<UserItem> c){
		_UserItemListVO vo = new _UserItemListVO();
		if(c == null || c.size() == 0){
		vo.setId(c.get(0).getId());
		vo.setUser(toClientUser(c.get(0).getUser()));
		ArrayList<_ConsumerProductVO> cl_products = new ArrayList<_ConsumerProductVO>();
		
		for(int i = 0; i < c.size(); i++){
			cl_products.add(toClientConsumerProduct(c.get(i).getConsumerProduct()));
		}
		
		vo.setConsumerProducts(cl_products);
		}
		return vo;
	}

	public static List<UserItem> toServerUserItemList(_UserItemListVO c){
		
		List <_ConsumerProductVO> products = c.getConsumerProducts();
		ArrayList<UserItem> userItems = new ArrayList<UserItem>();
		
		for(int i = 0; i < products.size(); i++){
			UserItem vo = new UserItem();
			vo.setId(c.getId());
			vo.setConsumerProducts(toServerConsumerProduct(products.get(i)));
			vo.setUser(toServerUser(c.getUser()));
			userItems.add(vo);
		}
		
		
		return userItems;	
	}
	
	
	public static _NameIdPairVO brandToNameId(Brand c){
		_NameIdPairVO vo = new _NameIdPairVO();
		vo.setId(c.getId());
		vo.setName(c.getName());
		return vo;
	}
	
	public static Brand nameIdToBrand(_NameIdPairVO c){
		Brand vo = new Brand();
		vo.setId(c.getId());
		vo.setName(c.getName());
		return vo;
	}

	public static Category nameIdToCategory(_NameIdPairVO c){
		Category vo = new Category();
		vo.setId(c.getId());
		vo.setName(c.getName());
		return vo;	
	}
	
	public static _NameIdPairVO categoryToNameId(Category c){
		_NameIdPairVO vo = new _NameIdPairVO();
		vo.setId(c.getId());
		vo.setName(c.getName());
		return vo;
	}
	

	public static Location nameIdToLocation(_NameIdPairVO c){
		Location vo = new Location();
		vo.setId(c.getId());
		vo.setLocation(c.getName());
		return vo;	
	}
	
	public static _NameIdPairVO locationToNameId(Location c){
		_NameIdPairVO vo = new _NameIdPairVO();
		vo.setId(c.getId());
		vo.setName(c.getLocation());
		return vo;	
	}
	
	public static Merchant nameIdToMerchant(_NameIdPairVO c){
		Merchant vo = new Merchant();
		vo.setId(c.getId());
		vo.setName(c.getName());
		return vo;		
	}
	
	public static _NameIdPairVO merchantToNameId(Merchant c){
		_NameIdPairVO vo = new _NameIdPairVO();
		vo.setId(c.getId());
		vo.setName(c.getName());
		return vo;	
	}

	public static Notes nameIdToNotes(_NameIdPairVO c){
		Notes vo = new Notes();
		vo.setId(c.getId());
		vo.setNote(c.getName());
		return vo;		
	}
	
	public static _NameIdPairVO notesToNameId(Notes c){
		_NameIdPairVO vo = new _NameIdPairVO();
		vo.setId(c.getId());
		vo.setName(c.getNote());
		return vo;	
	}

	public static List<ConsumerProduct> toServerConsumerProductList(List<_ConsumerProductVO> c){
		List<ConsumerProduct> sList = null;
		
		if(c != null){
			sList = new ArrayList<ConsumerProduct>();
			for(int i = 0; i < c.size(); i++){
				ConsumerProduct cp = toServerConsumerProduct(c.get(i));
				sList.add(cp);
			}
		} 
		
		return sList;
		
	}
	
	public static List<_ConsumerProductVO> toClientConsumerProductList(List<ConsumerProduct> c){
		List<_ConsumerProductVO> cList = null;
		
		if(c != null){
			cList = new ArrayList<_ConsumerProductVO>();
			for(int i = 0; i < c.size(); i++){
				_ConsumerProductVO cp = toClientConsumerProduct(c.get(i));
				cList.add(cp);
			}
		} 
		
		return cList;
		
	}
}
